package demo.mq.web;

import demo.mq.service.RedissonUtil;
import org.redisson.api.RLock;
import org.slf4j.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 *
 * @author 雷霆
 * @version 1.0.0
 * @date$ 2018/12/12
 *
 * Description：
 *
 * Modification History:
 *
 */
@Controller
public class RedisTestController {

    private static final Logger log = LoggerFactory.getLogger(RedissonUtil.class);

    @RequestMapping(value = "/redis",method = RequestMethod.GET)
    @ResponseBody
    public void test(String recordId) {

        RLock lock = RedissonUtil.getRLock(recordId);
        try {
            boolean bs = lock.tryLock(5, 6, TimeUnit.SECONDS);
            if (bs) {
                // 业务代码
                log.info("进入业务代码: " + recordId);

                lock.unlock();
            } else {
                Thread.sleep(300);
            }
        } catch (Exception e) {
            log.error("", e);
            lock.unlock();
        }
    }

}
